package TUILayer;
import java.util.Scanner;
import ControlLayer.*;
/**
 * Sale user interface:
 * Enables the user to create, find, delete, update and list all sales.
 * 
 * @author (Gruppe 3)
 */
/**
 * Sale user interface:
 * Enables the user to start a new sale, find a sale, delete a sale and list all sales
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SaleUI
{   
    // instance variables
    private SaleCtr saleCtr;
    private NewSaleUI newSaleUI;
    
    /**
     * Constructor for objects of class StorageUI
     */
    public SaleUI()
    {
        saleCtr = new SaleCtr();
    }
    
    /**
     * calls the method saleMenu() to open the menu
     */
    public void open()
    {
        saleMenu();
    }
    
    /**
     * method to make a choice in the menu
     */
    private void saleMenu()
    {
        boolean back = false;
        while(!back)
        {
            int choice = makeSaleMenu();
            
            if(choice == 1)
            {
                //Creates a new sale
                openNewSaleUI();
            }
            else
            {
                if(choice == 2)
                {
                    //Find a sale
                    findSale();
                }
                else
                {
                    if(choice == 3)
                    {
                        //Delete a sale
                        deleteSale();
                    }
                    else
                    {
                        if(choice == 4)
                        {
                            //Show all sales
                            showAllSales();
                        }
                        else
                        {
                           if(choice == 5)
                           {    
                               // go back to previous menu
                               back=true;
                           }
                           else
                           {
                                System.out.println("Indtast et nummer mellem 1 og 5");
                                waitInput();
                           }
                        }
                    }
                }
            }
           
        }
    }
    
    /**
     * prints the sale menu
     */
    private int makeSaleMenu()
    {   
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f ### Salg menu ### ");
        System.out.println(" (1) Opret salg");
        System.out.println(" (2) Find salg");
        System.out.println(" (3) Slet salg");
        System.out.println(" (4) Vis alle salg");
        System.out.println(" (5) Tilbage");
        System.out.println("\n Vælg et nummer");
        
        int choice = keyboard.nextInt();
        
        return choice;
    }
    
    /* 
     * opens the sale menu
     * waits for an input, so the user can read the screen output
     */
    private void openNewSaleUI()
    {
        newSaleUI = new NewSaleUI();
        newSaleUI.open();
    }
    
    public static void waitInput()
    {
        System.out.println("Tryk på en tast for at forsætte");
        new Scanner(System.in).nextLine();
    }
    
    /**
     * checks if the input is an integer
     * @param checkInput  string to check if it is an integer
     */ 
    private static boolean isInteger(String checkInput)
    {
        try
        {
            Integer.parseInt(checkInput);
        }
        catch(NumberFormatException e)
        {   
            System.out.println("Du skal skrive et heltal.");
            return false;
        }
        return true;
        
    }
    
    /**
     * inputs a variable of the type int
     */
    private static int inputInt()
    {
        Scanner keyboard = new Scanner(System.in); 
        boolean parsedInput = false;
        
        while(!parsedInput)
        {   
            String testInput = keyboard.nextLine();
            
            // checks if the input string is an integer
            if (isInteger(testInput))
            {   
                parsedInput = true;
                int newInput = Integer.parseInt(testInput);               
                return newInput;
            }
        }
           
        return 0;
    }

    
    /**
     * finds a sale
     */
    private void findSale()
    {   
        System.out.println("Skriv invoice nummer for at finde et salg");
        
        try
        {
            int searchInvoiceNumber = inputInt();
            saleCtr.findSale(searchInvoiceNumber);        
            waitInput();
        }
        catch(NullPointerException e)
        {
            System.out.println("Systemet kunne finde salget");
            waitInput();
        }
        
    }
    
    /**
     * deletes a sale
     */
    private void deleteSale()
    {   
        System.out.println("skriv invoice nummer for at slette et salg");
        int searchInvoiceNumber = inputInt();
        
        try
        {       
            saleCtr.deleteSale(searchInvoiceNumber);
            System.out.println("salget med invoice nummer: "+ searchInvoiceNumber + " blev fjernet");
            waitInput();
        }
        catch(NullPointerException e)
        {
            System.out.println("Systemet kunne ikke finde salget og kunne derfor ikke slette det");
            waitInput();
        }
        
    }
    
    /**
     * show all sales
     */
    private void showAllSales()
    {   
        try
        {
            saleCtr.listAllSales();        
            waitInput();
        }
        catch(NullPointerException e )
        {
            System.out.println("Der skete en fejl, da systemet prøvede at liste alle registered salg");
            waitInput();
        }
    } 
}
    
    
